<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="900px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      :inline="true"
    
      label-width="120px"
    >
      <el-form-item label="车牌号" prop="licenseNum">
        <el-input v-model="dataForm.licenseNum" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item size="mini" label="驾驶员">
        <el-popover
          ref="posiPopover"
          v-model="visibleTree"
          placement="bottom-start"
          trigger="click"
        >
          <el-tree
            :data="posiList"
            :props="posiListTreeProps"
            node-key="menuId"
            ref="menuListTree"
            @node-click="roleNodeClick"
          ></el-tree>
          <el-input
            slot="reference"
            v-model="dataForm.name"
            :readonly="true"
            placeholder="点击选择"
            class="menu-list__input"
          ></el-input>
        </el-popover>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="门禁识别卡">
        <el-input v-model="dataForm.idCard" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="闸机权限">
        <el-checkbox
          :indeterminate="isIndeterminate"
          v-model="checkAll"
          @change="handleCheckAllChange"
        >全选</el-checkbox>
        <el-checkbox-group
          v-model="checkedlabel"
          @change="handleCheckedCitiesChange"
          style="max-height:400px;overflow:auto"
        >
          <el-checkbox
            border
            size="mini"
            v-for="item in antuList"
            :key="item.id"
            :label="item.id"
            style="margin:5px;width:240px;overflow:hidden"
          >{{item.name}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from "@/utils";
export default {
  data() {
    return {
      visible: false,
      visibleTree: false,
      isIndeterminate: true,
      checkAll: false,
      dataId: "",
      dataForm: {},
      posiList: [],
      menuList: [],
      radio1: 0,
      antuList: [],
      checkedlabel: [],
      expendKey: [],
      menuListTreeProps: {
        children: "children",
        label: "name"
      },
      posiListTreeProps: {
        children: "children",
        label: "lable"
      },

      dataRule: {
        licenseNum: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ]
      },
      tempKey: -666666 // 临时key, 用于解决tree半选中状态项不能传给后台接口问题. # 待优化
    };
  },
  methods: {
    init(id) {
      this.dataForm = {
        id: "",
        licenseNum: "",
        name: "",
        phone: "",
        roleId: "",
        idCard: "",
        gateIds: "",
        isOut: "0"
      };
      this.checkedlabel = [];
      this.dataId = id || "";
      this.$http({
        url: this.$http.adornUrl("/sys/org/employee/list"),
        method: "POST",
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.posiList = data.list;
      });
      this.$http({
        url: this.$http.adornUrl("/sys/car/info"),
        method: "POST",
        params: this.$http.adornParams({
          id: this.dataId
        })
      }).then(({ data }) => {
        this.antuList = data.gates.filter(item => item.type == "0");
        if (data.entity) {
          Object.assign(this.dataForm, data.entity);
          if (data.entity.gateIds) {
            this.checkedlabel = data.entity.gateIds.split(",");
            this.handleCheckedCitiesChange(this.checkedlabel);
          }
        }
      });
      this.visible = true;
    },
    handleCheckedCitiesChange(data) {
      let checkedCount = data.length;
      this.checkAll = checkedCount === this.antuList.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.antuList.length;
    },
    handleCheckAllChange(val) {
      this.checkedlabel = val ? this.antuList.map(item => item.id) : [];
      this.isIndeterminate = false;
    },
    roleNodeClick(data) {
      if (data.type == "2") {
        Object.assign(this.dataForm, {
          employeeId: data.value,
          name: data.lable
        });
        this.visibleTree = false;
      }
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sys/car/update`),
            method: "post",
            data: this.$http.adornData({
              data: JSON.stringify(
                Object.assign(this.dataForm, {
                  id: this.dataId,
                  gateIds: this.checkedlabel.join(","),
                  isOut: 0
                })
              )
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
